//
//  MyReader.m
//  Runner
//
//  Created by 2021 on 12/23/21.
//

#import "MyReader.h"
#import <AVFoundation/AVSpeechSynthesis.h>

@interface MyReader()<AVSpeechSynthesizerDelegate>
@property(nonatomic, strong) AVSpeechSynthesizer *avSpeaker;
@property(nonatomic, strong) AVSpeechUtterance *utterance;
@end

@implementation MyReader
+(MyReader *)shared {
  static MyReader *manager = nil;
  static dispatch_once_t onceToken;
  dispatch_once(&onceToken, ^{
    manager = [[MyReader alloc] init];
    [manager initManager];
  });
  return manager;
}

+ (void)startRead:(NSString *)text {
    //开始朗读
    MyReader *reader = [self shared];
    [reader.utterance setValue:text forKey:@"speechString"];
    NSString *str = reader.utterance.speechString;
    if (str != nil && str.length > 0) {
        [reader.avSpeaker speakUtterance: reader.utterance];
    } else {
        [reader.avSpeaker speakUtterance: reader.utterance];
    }
    
}

+ (void)stopRead {
    //AVSpeechBoundaryImmediate 立即停止
    //AVSpeechBoundaryWord    当前词结束后停止
    MyReader *reader = [self shared];
    [reader.avSpeaker stopSpeakingAtBoundary:AVSpeechBoundaryImmediate];
}

- (void)initManager {
    _avSpeaker = [[AVSpeechSynthesizer alloc] init];
    _avSpeaker.delegate = self;
    //初始化要说出的内容
    AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc] initWithString:@"未能正确获取阅读信息"];
    //设置语速,语速介于AVSpeechUtteranceMaximumSpeechRate和AVSpeechUtteranceMinimumSpeechRate之间
    //AVSpeechUtteranceMaximumSpeechRate
    //AVSpeechUtteranceMinimumSpeechRate
    //AVSpeechUtteranceDefaultSpeechRate
    utterance.rate = 0.5;
            
    //设置音高,[0.5 - 2] 默认 = 1
    //AVSpeechUtteranceMaximumSpeechRate
    //AVSpeechUtteranceMinimumSpeechRate
    //AVSpeechUtteranceDefaultSpeechRate
    utterance.pitchMultiplier = 1;
            
    //设置音量,[0-1] 默认 = 1
    utterance.volume = 1;

    //读一段前的停顿时间
    utterance.preUtteranceDelay = 1;
    //读完一段后的停顿时间
    utterance.postUtteranceDelay = 1;
            
    //设置声音,是AVSpeechSynthesisVoice对象
    //AVSpeechSynthesisVoice定义了一系列的声音, 主要是不同的语言和地区.
    //voiceWithLanguage: 根据制定的语言, 获得一个声音.
    //speechVoices: 获得当前设备支持的声音
    //currentLanguageCode: 获得当前声音的语言字符串, 比如”ZH-cn”
    //language: 获得当前的语言
    //通过特定的语言获得声音
    AVSpeechSynthesisVoice *voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"zh-CN"];
    //通过voicce标示获得声音
    //AVSpeechSynthesisVoice *voice = [AVSpeechSynthesisVoice voiceWithIdentifier:AVSpeechSynthesisVoiceIdentifierAlex];
    utterance.voice = voice;
    _utterance = utterance;
}

#pragma mark -
#pragma mark - AVSpeechSynthesizerDelegate
//已经开始
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didStartSpeechUtterance:(AVSpeechUtterance *)utterance{


}
//已经说完
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance *)utterance{

//如果朗读要循环朗读，可以在这里再次调用朗读方法
//[_avSpeaker speakUtterance:utterance];
}
//已经暂停
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didPauseSpeechUtterance:(AVSpeechUtterance *)utterance{

}
//已经继续说话
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didContinueSpeechUtterance:(AVSpeechUtterance *)utterance{

}
//已经取消说话
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didCancelSpeechUtterance:(AVSpeechUtterance *)utterance{

}
//将要说某段话
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer willSpeakRangeOfSpeechString:(NSRange)characterRange utterance:(AVSpeechUtterance *)utterance{

}

@end
